package com.iqiyi.pps.epg.core.rpc.encode;

import com.iqiyi.pps.epg.api.model.web.encode.TEncodeServer;
import com.iqiyi.pps.epg.api.server.web.encode.EncodeServerWebService;
import com.iqiyi.pps.epg.core.service.encode.EncodeService;
import com.iqiyi.pps.epg.core.service.stream.LiveChannelStreamService;
import com.iqiyi.pps.epg.core.utils.ApplicationContextUtil;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuyankui on 15-12-9.
 */
public class EncodeWebServiceImpl implements EncodeServerWebService.Iface{

    public static Logger logger = LoggerFactory.getLogger(EncodeWebServiceImpl.class);
    @Override
    public long create(TEncodeServer entity) throws TException
    {
        long result = 0;

        if (entity != null) {
            EncodeService encodeService = (EncodeService) ApplicationContextUtil.getBean(EncodeService.class);

            long start1 = System.currentTimeMillis();
            result = encodeService.saveEncode(entity);
            long start2 = System.currentTimeMillis();
            logger.info("[EncodeWebServiceImpl][create][time={}]",(start2-start1));


            if(entity.getEncodeType() == 2) {
                LiveChannelStreamService liveChannelStreamService = (LiveChannelStreamService) ApplicationContextUtil.getBean(
                        LiveChannelStreamService.class);

                if(entity.getStatus() == -1) {
                    liveChannelStreamService.stopStream(entity.getOutputSignal());
                }else{
                    liveChannelStreamService.startStream(entity.getOutputSignal(), entity);
                }
            }
        }
        return  result;
    }

    @Override
    public boolean edit(TEncodeServer entity) throws TException {
        return false;
    }

    @Override
    public TEncodeServer getById(long id) throws TException {
        return null;
    }

    @Override
    public List<TEncodeServer> duBiStreamInfoSuggest(String streamName, int encodeType) throws TException {
        List<TEncodeServer> duBiInfo = new ArrayList<>();
        EncodeService encodeService = (EncodeService) ApplicationContextUtil.getBean(EncodeService.class);
        try {
            duBiInfo  = encodeService.duBiStreamInfoSuggest(streamName, encodeType);
        } catch (Exception e) {
            logger.error("[duBiStreamInfoSuggest][exception={}]", e);
        }
        return duBiInfo;
    }

    @Override
    public List<TEncodeServer> h254StreamInfoSuggest(String streamName) throws TException {
        List<TEncodeServer> duBiInfo = new ArrayList<>();
        EncodeService encodeService = (EncodeService) ApplicationContextUtil.getBean(EncodeService.class);
        try {
            duBiInfo  = encodeService.h265StreamInfoSuggest(streamName);
        } catch (Exception e) {
            logger.error("[h265StreamInfoSuggest][exception={}]", e);
        }
        return duBiInfo;
    }
}
